package com.warehouse.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
	
	private static String DRIVERCLASS;  //��������
	private static String URL;          //���ݿ������ַ���  
	private static String USER;         //�û���
	private static String PASSWORD;     //����
	
	/**
	 * ��̬����鹦�ܣ�ע�������;�̬������ֵ
	 * 
	 * @param ��
	 * @return ��
	 */
	static {
		DRIVERCLASS = "com.mysql.jdbc.Driver";
		URL = "jdbc:mysql://192.168.42.144:3306/warehouse_db";
		USER = "root";
		PASSWORD = "heart";
	}
	
	/**
	 * ���ܣ�������ݿ����Ӷ���
	 * @param ��
	 * @return conn ���ݿ����Ӷ���ʵ��
	 */
	public static Connection getConnection(){
		Connection conn = null; 
		try {
			Class.forName(DRIVERCLASS);
			conn = DriverManager.getConnection(URL,USER,PASSWORD);  //��ȡ���Ӷ���ʵ��
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * �������ܣ��ͷ����ݿ���Դ
	 * @param rs     ResultSet����ʵ��
	 * @param stm    Statement����ʵ��
	 * @param conn   Connection����ʵ��
	 * @return       ��
	 */
	public static void release(Connection conn, Statement stm, ResultSet rs){
		try {
			if(rs!=null){
				rs.close();
				rs = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		try {
			if(stm!=null){
				stm.close();
				stm = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		try {
			if(conn!=null){
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}
